//
//  MineSettingTableViewCell.swift
//  MT
//
//  Created by M on 2019/12/31.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MineSettingTableViewCell: UITableViewCell {
    
    var wdSettingType: MTWithdrawController.SettingType? {
        didSet {
            guard let type = wdSettingType else { return }
            titleLabel.text = type.title
            if type.isIndicatorNeeded {
                self.tintColor = .white
                self.accessoryType = .disclosureIndicator
            } else {
                self.accessoryType = .none
            }
            
//            if type.title == "实名认证" {
//                spaceView.backgroundColor = UIColor(hex: 0xf6f6f6)
//            } else {
//                spaceView.backgroundColor = .white
//            }
        }
    }
    
    var detailStr: String? {
        didSet {
            guard let detailStr = detailStr else { return }
            detailButton.setTitle(detailStr, for: .normal)
        }
    }
    
    var settingType: MineSettingViewController.SettingType? {
        didSet {
            guard let type = settingType else { return }
            titleLabel.text = type.title
            if let detail = type.detail as? String {
                detailButton.setTitle(detail, for: .normal)
            } else if let image = (type.detail as? UIImage)?.resize(to: CGSize(width: 30, height: 30)) {
                detailButton.setImage(image, for: .normal)
            }
            
            if type.isIndicatorNeeded {
                self.tintColor = .white
                self.accessoryType = .disclosureIndicator
            } else {
                self.accessoryType = .none
            }
            
        }
    }
    
    /// Title
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(14)
        label.textColor = UIColor(hex: 0x1d1a1a)
        label.textAlignment = .left
        return label
    }()
    
    /// Detail
    lazy var detailButton: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor(hex: 0x9b9b9b), for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(14)
        btn.adjustsImageWhenHighlighted = false
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    lazy var spaceView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: 0xf6f6f6)
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------------------------ Overrides ----------------------------------------
extension MineSettingTableViewCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
        detailButton.setTitle(nil, for: .normal)
        detailButton.setImage(nil, for: .normal)
    }
    
}

// MARK: ------------------------------------ 初始化 ----------------------------------------
extension MineSettingTableViewCell {
 
    private func initializeUI() {
        selectionStyle = .none
        backgroundColor = UIColor(hex: 0xffffff)
        contentView.backgroundColor = UIColor(hex: 0xffffff)
        contentView.addSubview(titleLabel)
        contentView.addSubview(detailButton)
//        contentView.addSubview(spaceView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(contentView)
            make.left.equalTo(contentView).offset(16)
        }
        detailButton.snp.makeConstraints { (make) in
            make.right.equalTo(contentView).offset(-16)
            make.centerY.equalTo(titleLabel)
        }
        
//        spaceView.snp.makeConstraints { (make) in
//            make.height.equalTo(10)
//            make.width.equalTo(ScreenWidth)
//            make.bottom.equalTo(contentView.snp.bottom).offset(10)
//        }
    }
    
}
